<?php
/**
 * Created by PhpStorm.
 * User: 夏浩文
 * Date: 2017/6/14
 * Time: 15:01
 */
namespace app\hongyanche\controller;

class forumAdmin
{
    private $auth;
    public function __construct()
    {
        model('product')->demand('forum');
        if (!model('admin')->is('forum')['state']) {
            $res = array('state' => 0, 'errormsg' => 'authority incorrect', 'errorcode' => '');
            $this->auth = $res;
        }else{
            $this->auth = array('state'=>1);
        }
    }

    //--------------------------板块管理------------------------------
    //添加板块
    public function label_add()
    {
        if (!empty($this->auth) && !$this->auth['state']) return json($this->auth);
        $cn = input('cn');
        $res = model('forum')->label_add($cn);
        return json($res);
    }
    //修改板块名
    public function label_rename()
    {
        if (!empty($this->auth) && !$this->auth['state']) return json($this->auth);
        $label_id = input('label_id');
        $newname = input('newname');
        $res = model('forum')->label_rename($label_id,$newname);
        return json($res);
    }
    //删除板块
    public function label_remove()
    {
        if (!empty($this->auth) && !$this->auth['state']) return json($this->auth);
        $label_id = input('label_id');
        $res = model('forum')->label_remove($label_id);
        return json($res);
    }
    //开放关闭板块
    public function label_release()
    {
        if (!empty($this->auth) && !$this->auth['state']) return json($this->auth);
        $release = input('release');
        $label_id = input('label_id');
        $res = model('forum')->label_release($label_id,$release);
        return json($res);
    }
    //模板列表
    public function label_list()
    {
        if (!empty($this->auth) && !$this->auth['state']) return json($this->auth);
        $res = model('forum')->label_list(null,1);
        return json($res);
    }
    //设置管理员活动板块
    public function setAdminlabel()
    {
        if (!empty($this->auth) && !$this->auth['state']) return json($this->auth);
        $label_id = input('label_id');
        $release = input('release');
        $res = model('forum')->setAdminlabel($label_id,$release);
        return json($res);
    }

    //--------------------------板块管理------------------------------
    //发布帖子
    public function forum_add()
    {
        if (!empty($this->auth) && !$this->auth['state']) return json($this->auth);
        $filter['title'] = input('title');
        $filter['content'] = input('content');
        $filter['label_id'] = input('label_id');
        $res = model('forum')->add($filter);
        return json($res);
    }
    //修改帖子
    public function forum_modify()
    {
        if (!empty($this->auth) && !$this->auth['state']) return json($this->auth);;
        $product_brand_id = input('forum_id');
        $key = input('key');
        $content = input('content');
        $res = model('forum')->modify($product_brand_id, $key, $content);
        return json($res);
    }
    //删除帖子
    public function forum_remove()
    {
        if (!empty($this->auth) && !$this->auth['state']) return json($this->auth);
        $brand_id = input('forum_id');
        $res = model('product')->remove($brand_id);
        return json($res);
    }
    //帖子列表
    public function forum_getlist()
    {
        if (!empty($this->auth) && !$this->auth['state']) return json($this->auth);
        $filer['page'] = input('page');
        $filer['maxPerPage'] = input('maxPerPage');
        $filer['type'] = input('type');
        $filer['label_id'] = input('label_id');
        $res = model('forum')->forum_list($filer);
        return json($res);
    }
    //开放、关闭
    public function forum_release()
    {
        if (!empty($this->auth) && !$this->auth['state']) return json($this->auth);
        $brand_id = input('forum_id');
        $release = input('release');
        $res = model('product')->release($brand_id, $release);
        return json($res);
    }

    //--------------------------申请------------------------------
    //申请列表
    public function apply_list()
    {
        if (!empty($this->auth) && !$this->auth['state']) return json($this->auth);
        $filer['page'] = input('page');
        $filer['maxPerPage'] = input('maxPerPage');
        $res = model('forum')->apply_list($filer);
        return json($res);
    }
    //审核
    public function apply_dispose()
    {
        if (!empty($this->auth) && !$this->auth['state']) return json($this->auth);
        $forum_id = input('forum_id');
        $approve = input('approve');
        $reason = input('reason');
        $label_id = input('label_id');
        $res = model('forum')->apply_dispose($forum_id,$approve,$reason,$label_id);
        return json($res);
    }

    //--------------------------积分管理------------------------------
    //添加积分规则
    public function rule_add()
    {
        if (!model('admin')->is('forum',1)['state']) return ['state'=>0,'errormsg'=>'您没有权限'];
        $data['way'] = input('way');
        $data['amount'] = input('amount');
        $res = model('forum')->addRule($data);
        return json($res);
    }
    //修改积分规则
    public function rule_modify()
    {
        if (!model('admin')->is('forum',1)['state']) return ['state'=>0,'errormsg'=>'您没有权限'];
//        $data['rule_id'] = input('rule_id');
//        $data['way'] = input('way');
//        $data['amount'] = input('amount');
//        $data['upper_limit'] = input('upper_limit');
//        $data['integral'] = input('integral');
//        $data['frequency'] = input('frequency');
        $id = input('rule_id');
        $key = input('key');
        $content = input('content');
        $res = model('forum')->rule_modify($id,$key,$content);
        return json($res);
    }
    public function rule_list()
    {
        if (!empty($this->auth) && !$this->auth['state']) return json($this->auth);
        $res = model('forum')->rule_list();
        return json($res);
    }

    //--------------------------版主管理------------------------------
    //邀请版主
    public function invite_moderator()
    {
        if (!empty($this->auth) && !$this->auth['state']) return json($this->auth);
        $label_id = input('label_id');
        $tel = input('tel');
        $res = model('forum')->invite_moderator($tel,$label_id);
        return json($res);
    }

    public function moderator_list()
    {
        $res = model('forum')->moderator_list();
        return json($res);
    }
    //撤销版主
    public function moderator_remove()
    {
        if (!empty($this->auth) && !$this->auth['state']) return json($this->auth);
        $label_id = input('label_id');
        $user_id = input('user_id');
        $res = model('forum')->moderator_remove($label_id,$user_id);
        return json($res);
    }
    //--------------------------数据管理------------------------------
    //数据分析
    public function dataAnalysis()
    {
        if (!empty($this->auth) && !$this->auth['state']) return json($this->auth);
        $data['start'] = input('start');
        $data['end'] = input('end');
        $res = model('forum')->dataAnalysis($data);
        return json($res);
    }
    //数据下载
    public function donwload()
    {
        if (!empty($this->auth) && !$this->auth['state']) return json($this->auth);
        $data['code'] = input('code');
        $data['start'] = input('start');
        $data['end'] = input('end');
        $res = model('forum')->download($data);
    }


    //------------------------转移板块---------------------------------
    public function changelabel(){
        $data['forum_id'] = input('forum_id');
        $data['label_id'] = input('label_id');
        $res = model('forum')->changelabel($data);
        return json($res);
    }
}
